package com.rambler.gateway;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import java.util.List;

/**
 * @author suhao
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Gateway_9527 {
    public static void main(String[] args) {
        SpringApplication.run(Gateway_9527.class, args);
    }

    @Resource
    private DiscoveryClient discoveryClient;

    @PostConstruct
    public void test() {
        List<String> services = discoveryClient.getServices();
        // 输出service, 拼接成一个字符串, 逗号分隔
        StringBuilder sb = new StringBuilder();
        for (String service : services) {
            sb.append(service).append(",");
        }
        System.out.println("所有服务: [" + sb + "]");
        List<ServiceInstance> instances = discoveryClient.getInstances("homelab-extension-service");
        for (ServiceInstance instance : instances) {
            System.out.println("serviceId: " + instance.getServiceId());
            System.out.println("host: " + instance.getHost());
            System.out.println("port: " + instance.getPort());
            System.out.println("uri: " + instance.getUri());
        }
    }
}

